package com.longcai.controller.clyjuser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.auth.SysShopsUser;
import com.longcai.entity.auth.vo.SysclyjuserVo;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.nearby.ClyjNearbyGoodsType;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.entity.nearby.ClyjNearbyShopsGoods;
import com.longcai.entity.sys.SysUser;
import com.longcai.mapper.clyjuser.ClyjUserMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjuser.ClyjUserService;
import com.longcai.util.OSSUtils;
import com.longcai.util.UserUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-03-31
 */
@RestController
@RequestMapping("/clyjUser")
public class ClyjUserController {

    @Autowired
    private ClyjUserService clyjUserService;
    @Autowired
    private ClyjUserMapper clyjUserMapper;

    @ApiOperation(value = "当前登陆用户")
    @GetMapping(value = "/current")
    public ClyjUser currentUser() {
        UserUtil.getLoginUser();
        QueryWrapper<ClyjUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", UserUtil.getLoginUser().getUserId());
        ClyjUser clyjUser = this.clyjUserMapper.selectOne(queryWrapper);
        return clyjUser;
    }

    /**
     * 获取会员列表
     *
     * @param clyjUser
     * @param pageHelper
     * @return
     */
    @ApiOperation(value = "会员列表")
    @GetMapping(value = "/clyjuserList")
    public PageTableResponse userList(ClyjUser clyjUser, PageHelper pageHelper) {

        return this.clyjUserService.userList(clyjUser, pageHelper);
    }


    /**
     * 开通导购会员列表
     *
     * @param clyjUser
     * @param pageHelper
     * @return
     */
    @ApiOperation(value = "会员列表")
    @GetMapping(value = "/clyjuserLists")
    public PageTableResponse userLists(ClyjUser clyjUser, PageHelper pageHelper) {

        return this.clyjUserService.userLists(clyjUser, pageHelper);
    }

    /**
     * 根据id获取会员信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取会员信息")
    @GetMapping("/{id}")
    public ApiResult user(@PathVariable String id) {

        return this.clyjUserService.getById(id);
    }

    /**
     * 修改会员信息
     *
     * @param clyjUser
     * @return
     */
    @PostMapping(value = "/updateUser")
    public Integer updateUser(SysclyjuserVo clyjUser, MultipartFile icon, MultipartFile CardFront, MultipartFile CardBack, MultipartFile CardHand) {
        if (icon != null) {
            //文档上传oss
            try {
                String fileName = icon.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = icon.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setPhoto(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (CardFront != null) {
            //文档上传oss
            try {
                String fileName = CardFront.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = CardFront.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setIdCardFront(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (CardBack != null) {
            //文档上传oss
            try {
                String fileName = CardBack.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = CardBack.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setIdCardBack(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (CardHand != null) {
            //文档上传oss
            try {
                String fileName = CardHand.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = CardHand.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setIdCardHand(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return this.clyjUserService.updateUser(clyjUser);
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @PostMapping("{id}")
    public void delClyjUser(@PathVariable String id) {
        this.clyjUserService.delClyjUser(id);
    }

    /**
     * 查询所有用户名
     *
     * @return
     */
    @RequestMapping("/getusername")
    @ResponseBody
    public List<ClyjUser> getusername() {

        return clyjUserService.getusername();
    }

    /**
     * 判断名称是否存在
     *
     * @param username
     * @return
     */
    @RequestMapping("/updatename")
    @ResponseBody
    public boolean updatename(String username) {
        ClyjUser u = this.clyjUserService.getName(username);
        if (u != null) {
            return true;
        }
        return false;
    }

    /**
     * 修改密码
     *
     * @param clyjUser
     * @return
     */
    @PutMapping(value = "updatePassword")
    @ApiOperation(value = "修改密码")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public ApiResult updatePassword(@RequestBody ClyjUser clyjUser) {
        return this.clyjUserService.updatePassword(clyjUser, clyjUser.getNewPassword());
    }

    /**
     * 忘记密码（修改密码）
     *
     * @param phone
     * @param code
     * @param password
     * @return
     * @paramnewpwd
     */
    @RequestMapping("/updatepwd")
    @ResponseBody
    public ApiResult updatePwd(String phone, String code, String password) {
        QueryWrapper<ClyjUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        ClyjUser user = this.clyjUserMapper.selectOne(queryWrapper);
        if (user == null) {
            return ApiResult.fail("该账号不存在");
        }
        if (code.equals("12345")) {
            return ApiResult.fail("验证码不正确");
        }
        return this.clyjUserService.updatePwd(phone, password);

    }


    /**
     * 查看用户信息
     *
     * @return
     */
    @PostMapping("/getuser")
    public ClyjUser getuser() {

        return clyjUserService.getuser();
    }


}

